package com.lujianfei.babymilkmemory.viewmodel

import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.lujianfei.babymilkmemory.compose.MainComposeType
import com.lujianfei.babymilkmemory.database.AppDatabase
import com.lujianfei.babymilkmemory.database.table.HistoryTable
import com.lujianfei.babymilkmemory.sp.Setting
import com.lujianfei.babymilkmemory.utils.MyLogger
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.util.*

/**
 * Author: mac
 * Date: 19.11.21 2:52 下午
 * Email:johnson@miaoshitech.com
 * Description: viewModel
 */
class MainViewModel: ViewModel() {

    // 上次喂奶时间字符串
    var lastTimeFeedStr by mutableStateOf("")
    // 距离上次喂奶时间字符串
    var differBetweenLastTimeFeedStr by mutableStateOf("")

    private var timer: Timer?= null
    // 上次喂奶时间
    private var lastTimeFeed:Long = 0

    /**
     * 启动定时器
     */
    fun startTimer(mainComposeType:MainComposeType) {
        if (timer != null) {
            timer?.cancel()
        }
        lastTimeFeed = when (mainComposeType) {
            MainComposeType.DRINK_MILK -> {
                Setting.INSTANCE.lastTime
            }
            MainComposeType.PEE -> {
                Setting.INSTANCE.lastTimePee
            }
            MainComposeType.DIAPER -> {
                Setting.INSTANCE.lastTimeDiaper
            }
        }
        if (lastTimeFeed == 0L) {
            when (mainComposeType) {
                MainComposeType.DRINK_MILK -> {
                    Setting.INSTANCE.lastTime = System.currentTimeMillis()
                    lastTimeFeed = Setting.INSTANCE.lastTime
                }
                MainComposeType.PEE -> {
                    Setting.INSTANCE.lastTimePee = System.currentTimeMillis()
                    lastTimeFeed = Setting.INSTANCE.lastTimePee
                }
                MainComposeType.DIAPER -> {
                    Setting.INSTANCE.lastTimeDiaper = System.currentTimeMillis()
                    lastTimeFeed = Setting.INSTANCE.lastTimeDiaper
                }
            }
        }
        updateLastTimeFeedStr(mainComposeType)
        timer = Timer()
        timer?.schedule(object :TimerTask() {
            override fun run() {
                // 距离上次时间喂奶的时差
                var day:Long = 0
                var hour:Long = 0
                var hourOnly:Long = 0
                var min:Long = 0
                var sec: Long

                val timeLong = System.currentTimeMillis() - lastTimeFeed
                sec = timeLong / 1000
                if (sec >= 60) {
                    min = sec / 60
                    sec %= 60
                }
                if (min >= 60) {
                    hour = min / 60
                    hourOnly = hour
                    min %= 60
                }
                if (hour >= 24) {
                    day = hour / 24
                    hour %= 24
                }

                differBetweenLastTimeFeedStr = when (mainComposeType) {
                    MainComposeType.DRINK_MILK,MainComposeType.DIAPER -> {
                        "$hourOnly 小时 $min 分 $sec 秒"
                    }
                    MainComposeType.PEE-> {
                        "$day 天 $hour 小时 $min 分 $sec 秒"
                    }
                }
            }
        },0, 1000)
    }

    /**
     * 更新上次喂奶时间
     */
    fun updateLastTimeFeed(mainComposeType:MainComposeType) {
        when (mainComposeType) {
            MainComposeType.DRINK_MILK -> {
                Setting.INSTANCE.lastTime = System.currentTimeMillis()
                lastTimeFeed = Setting.INSTANCE.lastTime
            }
            MainComposeType.PEE -> {
                Setting.INSTANCE.lastTimePee = System.currentTimeMillis()
                lastTimeFeed = Setting.INSTANCE.lastTimePee
            }
            MainComposeType.DIAPER -> {
                Setting.INSTANCE.lastTimeDiaper = System.currentTimeMillis()
                lastTimeFeed = Setting.INSTANCE.lastTimeDiaper
            }
        }
        updateDatabase(mainComposeType)
        updateLastTimeFeedStr(mainComposeType)
    }

    /**
     * 更新上次喂奶时间
     */
    fun updateLastTimeFeedTo(milliSec: Long, mainComposeType: MainComposeType) {
        when (mainComposeType) {
            MainComposeType.DRINK_MILK -> {
                Setting.INSTANCE.lastTime = milliSec
                lastTimeFeed = Setting.INSTANCE.lastTime
            }
            MainComposeType.PEE -> {
                Setting.INSTANCE.lastTimePee = milliSec
                lastTimeFeed = Setting.INSTANCE.lastTimePee
            }
            MainComposeType.DIAPER -> {
                Setting.INSTANCE.lastTimeDiaper = milliSec
                lastTimeFeed = Setting.INSTANCE.lastTimeDiaper
            }
        }
        updateDatabase(mainComposeType)
        updateLastTimeFeedStr(mainComposeType)
    }

    private fun updateDatabase(mainComposeType: MainComposeType) {
        viewModelScope.launch(Dispatchers.IO) {
            AppDatabase.instance().historyDao().insert(
                HistoryTable(
                    type = when (mainComposeType) {
                        MainComposeType.DRINK_MILK -> HistoryTable.Type.Milk
                        MainComposeType.PEE -> HistoryTable.Type.Pee
                        MainComposeType.DIAPER -> HistoryTable.Type.Diaper
                    }, create = (lastTimeFeed / 1000).toInt()
                )
            )
        }
    }

    /**
     * 获取上次喂奶时间
     */
    fun getLastTimeFeed(mainComposeType:MainComposeType):Long {
        return when (mainComposeType) {
            MainComposeType.PEE-> {
                Setting.INSTANCE.lastTimePee
            }
            MainComposeType.DIAPER-> {
                Setting.INSTANCE.lastTimeDiaper
            }
            else-> {
                Setting.INSTANCE.lastTime
            }
        }
    }

    /**
     * 关闭定时器
     */
    fun stopTimer() {
        timer?.cancel()
    }

    private fun updateLastTimeFeedStr(mainComposeType: MainComposeType) {
        val date = if (mainComposeType == MainComposeType.PEE)  SimpleDateFormat("MM月dd号\nHH:mm", Locale.getDefault()) else SimpleDateFormat("HH:mm", Locale.getDefault())
        lastTimeFeedStr = date.format(Date(lastTimeFeed))
    }
}